terraform {
  required_version = "~> 1.4.5"

  required_providers {
    maas = {
      # ~/.terraform.d/plugins/terraform.local/local/maas/1.1.0/darwin_arm64/terraform-provider-maas_v1.1.0
      source  = "terraform.local/maas/maas"
      version = "1.1.1"
    }
  }
}

provider "maas" {
  api_version = "2.0"
  api_key     = "8F5rTQr9EupWNsCEKz:YEuaxZBqSnDyT9kNCK:gkgBGDD4vMMANML6Hx6UeNWQUduYwHaz"
  api_url     = "http://10.3.8.6:5240/MAAS"
}

# provider "maas" {
#   api_version = "2.0"
#   api_key     = "u4JuZA5vWGEBfdkmuM:bZ849FpckHZUBxWEtF:fzHUgAvZFNGeT9xWLacMb5EvntE4yqgN"
#   api_url     = "http://10.3.8.62:5240/MAAS"
# }

resource "maas_resource_pool" "main" {
  name = "test-maas-pool"
  # description = "test"
}

# resource "maas_machine" "deeproute_bm1" {
#   hostname   = "10-3-8-63"
#   power_type = "ipmi"
#   power_parameters = {
#     power_address = "10.253.253.143"
#     power_user    = "deeproute"
#     power_pass    = "YS1^qwz@123"
#     power_boot_type = "efi"
#     power_driver    = "LAN_2_0"
#     privilege_level = "ADMIN"
#   }
# }

# data "maas_subnet" "pxe" {
#   cidr = "10.3.8.0/22"
# }

# data "maas_machine" "deeproute_bm1" {
#   system_id = maas_machine.deeproute_bm1.id
# }

# locals {
#   interfaces = [
#     for nic in data.maas_machine.deeproute_bm1.interfaces :
#     nic if nic.type == "physical" && nic.firmware_version != "CDC Ethernet Device"
#   ]
# }

# # note: Using for_each requires pre-creation of maas_machine due to terraform plan constraint
# #
# # resource "maas_network_interface_physical" "deeproute_bm1_nic" {
# #   machine = maas_machine.deeproute_bm1.id
# #   vlan    = "5004"
# #
# #   for_each    = toset([for nic in local.interfaces : nic.mac_address])
# #   mac_address = each.value
# # }

# resource "maas_network_interface_physical" "deeproute_bm1_nic0" {
#   machine = maas_machine.deeproute_bm1.id
#   vlan    = "5004"

#   mac_address = local.interfaces.0.mac_address
# }

# resource "maas_network_interface_physical" "deeproute_bm1_nic1" {
#   machine = maas_machine.deeproute_bm1.id
#   vlan    = "5004"

#   mac_address = local.interfaces.1.mac_address
# }

# resource "maas_network_interface_bond" "deeproute_bm1_bond4" {
#   machine               = maas_machine.deeproute_bm1.id
#   name                  = "bond4"
#   bond_miimon           = 120
#   bond_mode             = "802.3ad"
#   bond_xmit_hash_policy = "layer2+3"
#   # mac_address           = "04:3f:72:bd:86:52"
#   vlan                  = "5004"
#   parents               = [for nic in local.interfaces : nic.id]

#   # depends_on = [maas_network_interface_physical.deeproute_bm1_nic]
#   depends_on = [maas_network_interface_physical.deeproute_bm1_nic0, maas_network_interface_physical.deeproute_bm1_nic1]
# }

# resource "maas_network_interface_link" "deeproute_bm1_bond4" {
#   machine           = maas_machine.deeproute_bm1.id
#   network_interface = maas_network_interface_bond.deeproute_bm1_bond4.id
#   subnet            = data.maas_subnet.pxe.id
#   mode              = "STATIC"
#   ip_address        = "10.3.8.63"
#   default_gateway   = true

#   depends_on = [maas_network_interface_bond.deeproute_bm1_bond4]
# }

# # output "deeproute_bm1" {
# #   # value = data.maas_machine.deeproute_bm1
# #   value = [for nic in data.maas_machine.deeproute_bm1.interfaces: nic.name if nic.type == "physical" && nic.firmware_version != "CDC Ethernet Device"]
# # }

# # resource "maas_network_interface_link" "deeproute_bm1_nic1" {
# #   machine = maas_machine.deeproute_bm1.id
# #   network_interface = maas_network_interface_physical.deeproute_bm1_nic1.id
# #   subnet = data.maas_subnet.pxe.id
# #   mode = "STATIC"
# #   ip_address = "10.3.8.63"
# #   default_gateway = true
# # }

# # resource "maas_instance" "deeproute_bm1" {
# #   allocate_params {
# #     hostname = maas_machine.deeproute_bm1.hostname
# #   }
# #   deploy_params {
# #     distro_series = "ubuntu-2004-kube-v1.23.16-amd64-20G-2023-02-14-1676354987"
# #   }
# # }
